﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace MenuRegistration
{
    public partial class _Default : System.Web.UI.Page
    {
        private Sinacor.Infra.Tools.MenuRegistration objMenu = new Sinacor.Infra.Tools.MenuRegistration();

        protected void Page_Load(object sender, EventArgs e)
        {
            lblMessage.Text = string.Empty;
            if (!Page.IsPostBack)
            {
                BtnGravar.Attributes.Add("onClick","javascript:return(Validacao());");
                CarregaModulo();
                Limpar();
                CarregaGrid();
            }
        }

        private void CarregaMenuSuperior()
        {
            DataSet ds = objMenu.BuscaMenuSuperior(cboModulo.SelectedValue.ToString());

            DataRow row = ds.Tables[0].NewRow();

            row[0] = 0;
            row[1] = "Selecione...";

            ds.Tables[0].Rows.InsertAt(row, 0);

            cboMenuSuperior.DataSource = ds;
            cboMenuSuperior.DataTextField = "DS_MENU";
            cboMenuSuperior.DataValueField = "CD_MENU";
            cboMenuSuperior.DataBind();
        }

        private void CarregaModulo()
        {
            DataSet ds = objMenu.BuscaModulos();
            DataRow row = ds.Tables[0].NewRow();

            row[0] = "Selecione...";

            ds.Tables[0].Rows.InsertAt(row,0);

            cboModulo.DataSource = ds;
            cboModulo.DataTextField = "CD_SISTEMA";
            cboModulo.DataValueField = "CD_SISTEMA";
            cboModulo.DataBind();
            
        }

        private void CarregaFuncao()
        {
            DataSet ds = objMenu.BuscaFuncoes(cboModulo.SelectedValue.ToString());
            DataRow row = ds.Tables[0].NewRow();

            row[0] = 0;
            row[1] = "Selecione...";

            ds.Tables[0].Rows.InsertAt(row, 0);

            cboFuncao.DataSource = ds;
            cboFuncao.DataTextField = "NM_FUNCAO";
            cboFuncao.DataValueField = "CD_FUNCAO";
            cboFuncao.DataBind();
        }

        protected void cboMenuSuperior_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void cboModulo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboModulo.SelectedIndex > 0)
            {
                CarregaMenuSuperior();
                CarregaFuncao();
                PreencheResourceKey();
            }
            else
            {
                cboMenuSuperior.Items.Clear();
                cboFuncao.Items.Clear();
                TxtPreDescricao.Text = "";
            }
        }

        private void PreencheResourceKey()
        {
            try
            {
                string modulo = ConfigurationSettings.AppSettings[cboModulo.SelectedValue.ToString()].ToString();
                TxtPreDescricao.Text = modulo + ".Menu.Label.";                
            }
            catch
            {
                lblMessage.Text = "Sigla do Módulo não cadastrada no Web.config";
            }
        }
        protected void BtnLimpar_Click(object sender, EventArgs e)
        {
            Limpar();
            TxtAssembly.Focus();
        }

        private void Limpar()
        {
            TxtCodMenu.Text = "0";
            TxtAssembly.Text = "";
            TxtClasse.Text = "";
            TxtDescricao.Text = "";
            cboModulo.SelectedIndex = 0;
            cboMenuSuperior.Items.Clear();
            cboFuncao.Items.Clear();
            cboStatus.SelectedIndex = 1;
            TxtAcao.Text = "I";
            TxtPreDescricao.Text = "";
        }

        protected void BtnPesquisar_Click(object sender, EventArgs e)
        {
            TxtAssemblyPesq.Text = TxtAssembly.Text;
            TxtClassePesq.Text = TxtClasse.Text;
            TxtDescricaoPesq.Text = TxtDescricao.Text;

            if (cboFuncao.SelectedIndex > 0)
                TxtFuncaoPesq.Text = cboFuncao.SelectedValue;
            else
                TxtFuncaoPesq.Text = "";

            if (cboMenuSuperior.SelectedIndex > 0)
                TxtMenuSuperiorPesq.Text = cboMenuSuperior.SelectedValue;
            else
                TxtMenuSuperiorPesq.Text = "";

            if (cboModulo.SelectedIndex > 0)
                TxtModuloPesq.Text = cboModulo.SelectedValue;
            else
                TxtModuloPesq.Text = "";

            if (cboStatus.SelectedIndex > 0)
                TxtBloqueioPesq.Text = cboStatus.SelectedValue;
            else
                TxtBloqueioPesq.Text = "";


            CarregaGrid();
        }

        private void CarregaGrid()
        {
            GrdMenu.DataSource = objMenu.BuscaMenus(0, TxtDescricaoPesq.Text, int.Parse("0" + TxtMenuSuperiorPesq.Text), TxtFuncaoPesq.Text, TxtModuloPesq.Text, TxtClassePesq.Text, TxtAssemblyPesq.Text, TxtBloqueioPesq.Text);
            GrdMenu.DataBind();
        }

        protected void BtnGravar_Click(object sender, EventArgs e)
        {
            if (TxtAcao.Text == "I")
            {
                objMenu.IncluirMenu(TxtPreDescricao.Text + TxtDescricao.Text,int.Parse(cboMenuSuperior.SelectedValue.ToString()),cboFuncao.SelectedValue.ToString(),cboModulo.SelectedValue.ToString(),TxtClasse.Text, TxtAssembly.Text, cboStatus.SelectedValue.ToString());
                Limpar();
                CarregaGrid();
                TxtAssembly.Focus();
            }
            else
            {
                objMenu.AlterarMenu(int.Parse(TxtCodMenu.Text), TxtPreDescricao.Text + TxtDescricao.Text, int.Parse(cboMenuSuperior.SelectedValue.ToString()), cboFuncao.SelectedValue.ToString(), cboModulo.SelectedValue.ToString(), TxtClasse.Text, TxtAssembly.Text, cboStatus.SelectedValue.ToString());
                Limpar();
                CarregaGrid();
                TxtAssembly.Focus();
            }
        }

        protected void GrdMenu_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // reference the Delete Link Command Column.
                LinkButton db = (LinkButton)e.Row.Cells[8].Controls[0];
                db.OnClientClick = string.Format("return confirm('Deseja realmente Excluir?');");
            }
        }

        protected void GrdMenu_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                objMenu.ExcluirMenu(int.Parse(((GridView)sender).DataKeys[e.RowIndex].Value.ToString()));
                CarregaGrid();
            }
            catch (Exception ex)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script type='text/javascript'>alert('" + ex.Message.ToString().Replace("\n","") + "');</script>");
            }
        }

        protected void GrdMenu_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

        }

        protected void GrdMenu_RowEditing(object sender, GridViewEditEventArgs e)
        {
            Limpar();

            DataSet ds = objMenu.BuscaMenus(int.Parse(((GridView)sender).DataKeys[e.NewEditIndex].Value.ToString()), "", 0, "", "", "", "", "");

            TxtAssembly.Text = ds.Tables[0].Rows[0].Field<string>("DS_ASSEMBLY");
            TxtClasse.Text = ds.Tables[0].Rows[0].Field<string>("DS_CLASSE");
            string descricaoMenu = ds.Tables[0].Rows[0].Field<string>("DS_MENU");
            string[] vetDescricaoMenu = descricaoMenu.Split('.');
            TxtDescricao.Text = vetDescricaoMenu[vetDescricaoMenu.Count() - 1].ToString();
            TxtPreDescricao.Text = descricaoMenu.Replace(TxtDescricao.Text, "");

            cboModulo.SelectedValue = ds.Tables[0].Rows[0].Field<string>("CD_SISTEMA");
            
            CarregaFuncao();
            CarregaMenuSuperior();

            try
            {
                cboFuncao.SelectedValue = ds.Tables[0].Rows[0].Field<string>("CD_FUNCAO"); 
            }
            catch{}

            try
            {
                cboMenuSuperior.SelectedValue = ds.Tables[0].Rows[0].Field<decimal>("CD_MENUSUP").ToString();
            }
            catch { }

            cboStatus.SelectedValue = ds.Tables[0].Rows[0].Field<string>("IN_BLOQUEIO");
            TxtCodMenu.Text = ds.Tables[0].Rows[0].Field<decimal>("CD_MENU").ToString();
            TxtAcao.Text = "A";
        }

        protected void GrdMenu_RowCreated(object sender, GridViewRowEventArgs e)
        {

        }
    }
}
